package com.ykzhujiang.lovespirit.util;

import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

import javax.cache.Cache;
import javax.cache.CacheException;
import javax.cache.CacheFactory;
import javax.cache.CacheManager;
import javax.jdo.PersistenceManager;

public class DataManager {

	public static boolean addData(Object o) {
		boolean ret = true;
		PersistenceManager pm = PMF.get().getPersistenceManager();
		pm.setMultithreaded(true);
		try {
			logger.info("try to add data");
			pm.makePersistent(o);
		} catch (Exception e) {
			ret = false;
			logger.warning(e.getMessage());
		} finally {
			pm.close();
			logger.info("close pm");
		}
		return ret;
	}

	public static boolean removeData(Class c, Object key) {
		boolean ret = true;
		PersistenceManager pm = PMF.get().getPersistenceManager();
		pm.setMultithreaded(true);
		try {
			logger.info("try to remove data");
			pm.deletePersistent(pm.getObjectById(c, key));
		} catch (Exception e) {
			ret = false;
			logger.warning(e.getMessage());
		} finally {
			pm.close();
			logger.info("close pm");
		}
		return ret;
	}

	public static Object getData(Class c, Object key, boolean cached, Map props) {
		Object result = null;
		Cache cache = null;
		logger.info("try to get data");
		try {
			CacheFactory cacheFactory = CacheManager.getInstance()
					.getCacheFactory();
			cache = cacheFactory.createCache(props);

		} catch (CacheException e) {
			logger.warning(e.getMessage());
		}
		String ckey = MEM_KEY_HEAD + "_" + key;
		if (cached && cache != null && cache.containsKey(ckey)) {
			logger.info("cache hit:" + ckey);
			result = cache.get(ckey);
		} else {
			PersistenceManager pm = PMF.get().getPersistenceManager();
			pm.setMultithreaded(true);
			pm.setDetachAllOnCommit(true);
			try {
				logger.info("try to get data:" + c + "," + key);
				result = pm.getObjectById(c, key);
				if (cached) {
					logger.info("try to cache:" + ckey);
					cache.put(ckey, result);
				}
			} catch (Exception e) {
				logger.warning(e.getMessage());
			} finally {
				logger.info("close pm");
				pm.close();
			}
		}
		return result;
	}

	public static Object getData(String query, boolean cached, Map props) {
		Object result = null;
		Cache cache = null;
		logger.info("try to get data");
		try {
			CacheFactory cacheFactory = CacheManager.getInstance()
					.getCacheFactory();
			cache = cacheFactory.createCache(props);

		} catch (CacheException e) {
			logger.warning(e.getMessage());
		}
		String key = MEM_KEY_HEAD + query;
		if (cached && cache != null && cache.containsKey(key)) {
			logger.info("cache hit:" + key);
			result = cache.get(key);
		} else {
			PersistenceManager pm = PMF.get().getPersistenceManager();
			pm.setMultithreaded(true);
			try {
				pm.setDetachAllOnCommit(true);
				result = pm.newQuery(query).execute();
				if (result != null)
					((List) result).size();
				if (cached) {
					logger.info("try to cache:" + key);
					cache.put(key, result);
				}
			} catch (Exception e) {
				logger.warning(e.getMessage());
			} finally {
				logger.info("close pm");
				pm.close();
			}
		}
		return result;
	}

	private DataManager() {

	}

	private static final Logger logger = Logger.getLogger(DataManager.class
			.getName());

	private static final String MEM_KEY_HEAD = "MEM_KEY_";
}
